package lol.clann.bukkit.pluginboot.model;

import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;

import java.util.Objects;

/**
 * @author pyz
 * @date 2018/9/19 下午8:42
 */
@Getter
public class BlockPos {

    private final int x, y, z;
    private final String world;
    private final int hash;

    private BlockPos(String world, int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.world = world;
        this.hash = Objects.hash(x, y, z, world);
    }

    public static BlockPos of(Item item) {
        return of(item.getLocation());
    }

    public static BlockPos of(Entity entity) {
        return of(entity.getLocation());
    }

    public static BlockPos of(Block block) {
        return of(block.getLocation());
    }

    public static BlockPos of(Location location) {
        return of(location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
    }

    public static BlockPos of(String world, int x, int y, int z) {
        return new BlockPos(world, x, y, z);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BlockPos blockPos = (BlockPos) o;
        return x == blockPos.x &&
                y == blockPos.y &&
                z == blockPos.z &&
                Objects.equals(world, blockPos.world);
    }

    @Override
    public int hashCode() {
        return hash;
    }
}
